
--[[
行会探险
]]


GUILDADVENTURE_IDX = 1 			--行会宝藏探险的IDX

--[[
注册一些事件
]]
function ResisterGuildAdventureProc()
	--print("ResisterGuildAdventureProc...")
	for advIdx, advCfg in ipairs( GuildConfig.guildAdventure.adventures ) do
		if advCfg.fubenId > 0 then
			SetNeedReloginFuben( advCfg.fubenId, GlobalLuaConfig.reloginFuben.guildAdventureIdx, GetElemsForReLoginGuildAdventure )		
			--该副本是需要掉线恢复的
			SceneExitDispatcher.register( advCfg.sceneId, OnCommonSceneExit )							--注册离开场景、副本后触发的事件
			SceneHandExitDispatcher.register( advCfg.sceneId, OnCommonSceneHandExit )					--注册手工离开场景、副本后触发的事件
			FubenTimeoutDispatcher.register( advCfg.fubenId, OnSingleCommonFubenTimeout )				--注册单人场景、副本时间到后触发的事件
			SceneLogoutDispatcher.register( advCfg.sceneId, OnCommonFubenLogout )						--注册场景、副本中下线触发的事件
		end
	end
end

function IsInGuildAdventure( fubenId )
	for advIdx, advCfg in ipairs( GuildConfig.guildAdventure.adventures ) do
		if advCfg.fubenId == fubenId then
			return true
		end
	end
	return false
end

function GetGuildAdventureCfg( fubenId )
	for advIdx, advCfg in ipairs( GuildConfig.guildAdventure.adventures ) do
		if advCfg.fubenId == fubenId then
			return advCfg
		end
	end
	return nil
end

--[[
为副本掉线恢复，获取一些数据
]]
function GetElemsForReLoginGuildAdventure( fubenPtr )
	local fubenId 		= Fuben.getFuBenId(fubenPtr)
	local fubenCfg 		= GetGuildAdventureCfg( fubenId )
	local InitFubenRightPanel = InitGuildAdventurePanel
	local loginLog   	= "guild adventure login" 
	local loginLogOk	= "guild adventure login ok"
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end

--[[
开始行会探险
]]
function EnterGuildAdventure( sysarg, advIdx )
	--print("EnterGuildAdventure, advIdx="..advIdx)
	local advCfg = GuildConfig.guildAdventure.adventures[advIdx]
	if not advCfg then
		return
	end

	if CommonEnterScence( sysarg, advCfg, 0, "", GuildAdventureCheck, GuildAdventureBeforeEnter ) then
		local fubenPtr = Actor.getFubenPrt( sysarg )
		Fuben.setFbValue( fubenPtr, GUILDADVENTURE_IDX, advCfg.advIdx )

		local actorId = Actor.getActorId( sysarg )
		Fuben.setOwnerId( fubenPtr, actorId )

		local fubenHandle = Actor.getFubenHandle( sysarg )
		FreshCommonMonsters( advCfg.monsters, fubenHandle, true )

		InitGuildAdventurePanel( sysarg, advCfg )
	end
end

function InitGuildAdventurePanel( sysarg, advCfg )
	local contStyle 	= 1
	local buttons 		= {1}
	local activityId 	= enRightPanelActivity_GuildAdventure
	local subActivityId = advCfg.advIdx
	local title			= advCfg.name
	OpenCommonStylePanel( sysarg, 1, activityId, subActivityId, title, advCfg, contStyle, buttons  )
end

function GuildAdventureCheck( sysarg, bossCfg )
	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_GuildAdventure, 0 ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0021, ttFlyTip)
		return false
	end
	return true
end

function GuildAdventureBeforeEnter( sysarg, bossCfg )
	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_GuildAdventure, 0, 1 ) 				--进入次数+1
	return true
end

--[[
行会怪物被击杀
]]
function OnGuilAdventureMonDie( monPtr, sysarg, monId, lastHitKiller )
	local fubenPtr 		= Actor.getFubenPrt( sysarg )
	local fubenHandle 	= Actor.getFubenHandle( sysarg )
	local advIdx   = Fuben.getFbValue( fubenPtr, GUILDADVENTURE_IDX )
	--print("OnGuilAdventureMonDie, monId="..monId..", advIdx="..advIdx)

	local advCfg = GuildConfig.guildAdventure.adventures[advIdx]
	if not advCfg then
		return
	end

	local activitySts = nil
	local contStyle 	= 1
	local buttons1 		= {1}
	local buttons2 		= {1}
	ChangeCommonStylePanel( sysarg, activitySts, contStyle, advCfg, buttons1, buttons2 )

	local allBoss, killBoss, allMon, killMon = GetCommonBossMonsterNum6( fubenHandle, advCfg.monsters )
	if killBoss + killMon >= allBoss + allMon then 		--击杀成功
		OnGuildAdventureOver( sysarg, advCfg, 1 )
	end
end

--[[游戏通关
sts:0-失败，1-成功
]]
function OnGuildAdventureOver( sysarg, advCfg, sts )
	--print("OnGuildAdventureOver, sts="..sts)
	local panelInfo = {}
	panelInfo.subActivityId = advCfg.advIdx
	panelInfo.sts 			= sts 			--这里是通过面板的状态

	if sts == 1 then
		panelInfo.awards = advCfg.awards
		GiveCommonAward( sysarg, advCfg.awards, GameLog.clGuildAdventureAward, Lang.ScriptTips.GuildAdvLog01 )
	end
	PassActivityRightPanel( sysarg, 1, enRightPanelActivity_GuildAdventure, panelInfo )
	Actor.exitFuben( sysarg )
end

ResisterGuildAdventureProc()
